<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input style="width:180px;" clearable placeholder="返厂批次号" v-model="searchDto.batch"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input clearable placeholder="快递单号" v-model="searchDto.logisticsNumber" ></el-input>
      </el-form-item>
      <el-form-item size="5">
        <el-input clearable placeholder="YSTSN" v-model="searchDto.ystsn"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="batchRepair()">批量维修</el-button>
      </el-form-item>
    </el-form>
        <custom-table
          :multipleSelection="roleType === '4'"
          :data="acceptWorkOrderList"
          :columns="columns"
          :tableLoadingFlag="tableLoadingFlag"
          :pagination="pagination"
          @selection-change="selectionChange">
        </custom-table>
  </div>
</template>

<script>
  import CustomTable from '@/components/table/CustomTable'
  import { apiGetDevicesRepairList, apiPostBatchRepair } from '@/api/repairs/factory/index'
  import { parseTime } from '@/utils'
  import { getRoleType, getName } from '@/utils/token'
  import {getUserId} from '../../../utils/token'

  export default {
    components: {
      CustomTable
    },
    created () {
      let listQuery = {
        pageNumber: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        isReceived: 0,
        status: 0
      }
      this.getAcceptWorkOrderList(listQuery)
      // 不是工厂操作按钮隐藏
      if (getRoleType() !== '4') {
        this.operationColumn = null
      }
      this.roleType = getRoleType()
    },
    data () {
      return {
        // tab标签页
        tableLoadingFlag: false,          // 已接收批次列表
        columns: [
          {label: '返厂批次号', name: 'batch', align: 'center'},
          {label: '品牌', name: 'brandName', align: 'center'},
          {label: '型号', name: 'modelName', align: 'center'},
          {label: 'YSTSN', name: 'ystsn', align: 'center', width: '180'},
          {label: '快递单号', name: 'logisticsNumber', align: 'center'},
          {label: '快递公司', name: 'logisticsCompany', align: 'center'},
          {label: '返厂时间', name: 'backTime', align: 'center', formatter: this.formatTime},
          {label: '省份', name: 'province', align: 'center'},
          {label: '地市', name: 'city', align: 'center'},
          {label: '返厂人姓名', name: 'sender', align: 'center'},
          {label: '联系电话', name: 'tel', align: 'center'},
          {label: '备注', name: 'remark', align: 'center'}
        ],
        acceptWorkOrderList: [],
        // 选择框
        selectionList: [],
        pagination: this.getData(this.getAcceptWorkOrderList),
        searchDto: {
          batch: undefined,
          logisticNumber: undefined,
          ystsn: undefined
        },
        listQuery: {
          isReceived: 0,
          batch: undefined,
          logisticsNumber: undefined,
          status: 0
        }
      }
    },
    methods: {
      // 日期格式
      formatTime (row, column, cellValue) {
        return parseTime(cellValue)
      },
      batchRepair() {
        if (this.selectionList === undefined || this.selectionList.length === 0) {
          this.$message({
            showClose: true,
            message: '未选中维修设备',
            type: 'warn'
          })
          return
        }
        this.$confirm('此操作维修设备:' + this.selectionList.length + '台 ' + '确认维修', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = []
          let batches = []
          let factoryIds = []
          let modelIds = []
          let brandIds = []
          let ystsns = []
          for (let i in this.selectionList) {
            ids.push(this.selectionList[i].id)
            batches.push(this.selectionList[i].batch)
            factoryIds.push(this.selectionList[i].factoryId)
            modelIds.push(this.selectionList[i].modelId)
            brandIds.push(this.selectionList[i].brandId)
            ystsns.push(this.selectionList[i].ystsn)
          }
          let listQuery = {
            batches: batches,
            factoryIds: factoryIds,
            modelIds: modelIds,
            brandIds: brandIds,
            ystsns: ystsns,
            ids: ids,
            repairerId: getUserId(),
            repairer: getName(),
            reason: ''
          }
          apiPostBatchRepair(listQuery).then(() => {
            this.$message({
              showClose: true,
              message: '维修成功',
              type: 'success'
            })
            let query = {
              pageNumber: this.pagination.currentPage,
              pageSize: this.pagination.pageSize
            }
            this.getAcceptWorkOrderList(query)
          })
        })
      },
      // 获取一览信息
      getAcceptWorkOrderList (listQuery) {
        this.tableLoadingFlag = true
        if (this.listQuery.batch !== undefined) {
          listQuery.batch = this.listQuery.batch
        }
        if (this.listQuery.logisticsNumber !== undefined) {
          listQuery.logisticsNumber = this.listQuery.logisticsNumber
        }
        if (this.listQuery.isReceived !== undefined) {
          listQuery.isReceived = this.listQuery.isReceived
        }
        if (this.listQuery.status !== undefined) {
          listQuery.status = this.listQuery.status
        }
        if (this.listQuery.ystsn !== undefined) {
          listQuery.ystsn = this.listQuery.ystsn
        }
        apiGetDevicesRepairList(listQuery).then(response => {
          this.acceptWorkOrderList = response.data
          this.pagination.totalRecords = response.totalRecords
          this.tableLoadingFlag = false
        }).catch(() => {
          this.tableLoadingFlag = false
        })
      },
      selectionChange (val) {
        this.selectionList = val
      },
      searchData () {
        this.listQuery.isReceived = 0
        this.listQuery.status = 0
        this.listQuery.batch = this.searchDto.batch
        this.listQuery.logisticsNumber = this.searchDto.logisticsNumber
        this.listQuery.ystsn = this.searchDto.ystsn
        this.pagination.currentPage = 1
        this.pagination.pageSize = 10
        this.getAcceptWorkOrderList(this.listQuery)
      }
    }
  }
</script>
